package save
{
	import event.UIEditsEventName;
	
	import fl.controls.Button;
	import fl.controls.Label;
	import fl.controls.RadioButton;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.utils.describeType;
	
	import kingsAsLib.events.EventManager;
	import kingsAsLib.events.KEvents;
	import kingsAsLib.events.KEvents;

	/**
	 * @author kings
	 * @E-mail: qywangweiqiang@126.com
	 * 2012-12-1上午03:11:20
	 * */
	public class DecodeAS
	{
		static private var decodeAs:DecodeAS=new DecodeAS();

		//需要全局的变量名
		private var variableNameWholeArr:Vector.<NameWholeArrStructure>=new Vector.<NameWholeArrStructure>;

		public function DecodeAS()
		{
			initLisnter();
		}

		private function initLisnter():void
		{
			// 
			EventManager.addEventListener(UIEditsEventName.variableWholeChange, variableWholeChangeHandle);
		}

		private function variableWholeChangeHandle(e:KEvents):void
		{
			// 
			var nameWhole:NameWholeArrStructure=e.getData();

			if(nameWhole.variableName.indexOf("instance")!=-1){
				return;
			}

			for (var i:int=0; i < variableNameWholeArr.length; i++)
			{
				if (variableNameWholeArr[i].variableName == nameWhole.variableName)
				{
					variableNameWholeArr[i].wholeStr=nameWhole.wholeStr;
					return;
				}
			}
			variableNameWholeArr.push(nameWhole);

		}

		static public function instance():DecodeAS
		{
			return decodeAs;
		}

		public function decodeObj(dis:DisplayObjectContainer):void
		{
			CodeInfo.instance().classContentStr=decodeCurrentDis(dis);
			SaveCodeWins.instance().setCodeStr(CodeInfo.instance().classAllStr);
		}

		private function decodeCurrentDis(dis:DisplayObjectContainer):String
		{
			var type:XML=describeType(dis);

			var temp:String=type.attribute("name");
			var index:int=temp.indexOf("::");
			var classSort:String=temp.substr(index + 2);
			var importClass:String=temp.substring(0, index) + "." + classSort;

			CodeInfo.instance().importStr=importClass;

			var classContenStr:String="";

			var tempVariableName:String=getVariableName(dis);

			if (isWholeVar(tempVariableName) != null)
			{
				var tempWholeVar:String="\n" + isWholeVar(tempVariableName) + " var " + tempVariableName + ":" + classSort + "= new " + classSort + "()" + ";\n";
				CodeInfo.instance().wholeVar+=tempWholeVar;
				classContenStr+="\n";
			}
			else
			{
				classContenStr+="\n var " + tempVariableName + ":" + classSort + "= new " + classSort + "()" + ";\n";
			}
			classContenStr+=tempVariableName + ".x=" + dis.x + ";\n";
			classContenStr+=tempVariableName + ".y=" + dis.y + ";\n";
			if ("Sprite" != classSort)
			{
				classContenStr+=tempVariableName + ".width=" + dis.width + ";\n";
				classContenStr+=tempVariableName + ".height=" + dis.height + ";\n";
			}


			classContenStr+=getOtherProperty(dis);


			if ("Sprite" == classSort)
			{
				if (dis.numChildren > 0)
				{
					for (var i:int=0; i < dis.numChildren; i++)
					{
						var tempAtDis:DisplayObjectContainer=dis.getChildAt(i) as DisplayObjectContainer;
						classContenStr+=decodeCurrentDis(tempAtDis) + "\n";
						classContenStr+=tempVariableName + ".addChild( " + getVariableName(tempAtDis) + ")" + ";\n";
					}
				}
			}

			return classContenStr;
		}

		private function getOtherProperty(dis:DisplayObjectContainer):String
		{
			var resultStr:String="";
			var type:XML=describeType(dis);

			var temp:String=type.attribute("name");
			var index:int=temp.indexOf("::");
			var classSort:String=temp.substr(index + 2);
			var tempVariableName:String=getVariableName(dis);
			switch (classSort)
			{
				case "Button":
				{
					resultStr+=tempVariableName + ".label=" + "'" + dis["label"] + "'";
					break;
				}
				case "CheckBox":
				{
					resultStr+=tempVariableName + ".label=" + "'" + dis["label"] + "'";
					break;
				}
				case "RadioButton":
				{
					resultStr+=tempVariableName + ".label=" + "'" + dis["label"] + "'";
					break;
				}
				case "Label":
				{
					resultStr+=tempVariableName + ".text=" + "'" + dis["text"] + "'";
					break;
				}
				case "TextInput":
				{
					resultStr+=tempVariableName + ".text=" + "'" + dis["text"] + "'";
					break;
				}
				case "TextArea":
				{
					resultStr+=tempVariableName + ".text=" + "'" + dis["text"] + "'";
					break;
				}


				default:
				{
					break;
				}
			}

			return resultStr;
		}

		private function getVariableName(dis:DisplayObject):String
		{

			var type:XML=describeType(dis);

			var temp:String=type.attribute("name");
			var index:int=temp.indexOf("::");
			var classSort:String=temp.substr(index + 2);

			var str:String="";
			if (dis.name.indexOf("instance") != -1)
			{
				str=classSort + "x_" + dis.x + "y_" + dis.y;
			}
			else
			{
				str=dis.name;
			}
			return str;
		}

		private function isWholeVar(_name:String):String
		{
			for (var i:int=0; i < variableNameWholeArr.length; i++)
			{
				if (variableNameWholeArr[i].variableName == _name)
				{
					return variableNameWholeArr[i].wholeStr;
				}

			}

			return null;
		}


	}

	//end class
}

